/* eslint-disable eqeqeq */
import actionInfo from '../action'
import { orderAdd, orderQuery } from '../service/orderService'

function newOrderReducer(state = { newOrder: {}, productList: { info: [] } }, action) {
    switch (action.type) {
        case actionInfo.neworder.ADD:
            return { newOrder: { ...state.newOrder, ...action.payload }, productList: { info: [] } }
        case actionInfo.neworder.CHANGE:
            return { newOrder: state.newOrder, productList: { info: [...state.productList.info, action.payload] } }
        case actionInfo.neworder.EMPTYALL:
            return { newOrder: {}, productList: { info: [] } }
        case actionInfo.neworder.EMPTY:
            return { newOrder: state.newOrder, productList: { info: [] } }
        case actionInfo.neworder.QUERY:
            return { newOrder: {}, productList: { info: [...action.payload] } }
        default:
            return state
    }
}

export const productListAddProduct = (payload) => {
    return {
        type: actionInfo.neworder.CHANGE,
        payload
    }
}
export const newOrderAdd = (payload) => {
    return {
        type: actionInfo.neworder.ADD,
        payload
    }
}
export const asyncOrderQuery = (params) => async (dispatch) => {
    let { data } = await orderQuery(params);//做异步请求
    if (data.code == 1) {
        dispatch(newOrderQuery(data.list));
    }

}
const newOrderQuery = (payload) => {
    return {
        type: actionInfo.neworder.QUERY,
        payload
    }
}
export const asyncOrderAdd = (params) =>  (dispatch) => {
    params.forEach(async item => {
        let { data } = await orderAdd(item);//做异步请求
        if (data.code == 1) {
            dispatch(orderInfoAdd());
        }
    })
}
export const orderInfoEmpty = (payload) => {
    return {
        type: actionInfo.neworder.EMPTY,
        payload
    }
}
const orderInfoAdd = (payload) => {
    return {
        type: actionInfo.neworder.EMPTYALL,
        payload
    }
}
export default newOrderReducer